package com.cuiweiyou.gimmick;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowEvent;
import java.util.List;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;

public class EditGimmickUI extends JFrame {
    private JFrame parentWindow;

    private int frameWidth;
    private int frameHeight;

    private String side;
    private boolean isUpdateYes = false;

    private GimmickBean gimmickBean;
    private JTextArea gimmickTextArea;
    private JTextArea lastIndexsTextArea;
    private JTextArea nextIndexsTextArea;
    private JRadioButton otherSideRadioBtn;
    private JRadioButton meSideRadioBtn;
    private JButton okButton;

    private OnEditGimmickUIClosedListener onEditGimmickUIClosedListener;

    public EditGimmickUI(JFrame frame, GimmickBean gimmick, OnEditGimmickUIClosedListener listener) {
        super("编辑条目");
        System.out.println("创建编辑弹窗");

        this.gimmickBean = gimmick;
        this.parentWindow = frame;
        this.parentWindow.setEnabled(false);
        this.onEditGimmickUIClosedListener = listener;
        isUpdateYes = false;

        setDefaultLookAndFeelDecorated(true);
        setAlwaysOnTop(true);

        Dimension dimension = Toolkit.getDefaultToolkit()
                                     .getScreenSize();
        frameWidth = (int) (dimension.getWidth() / 2);
        frameHeight = (int) (dimension.getHeight() / 2);
        setSize(frameWidth, frameHeight);
        int x = (int) (dimension.getWidth() / 2 - frameWidth / 2);
        int y = (int) (dimension.getHeight() / 2 - frameHeight / 2);
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        setResizable(false);
        setLocation(x, y);
        setSize(frameWidth, frameHeight);

        initView();
        initEvent();

        setVisible(true);
    }

    private void initEvent() {
        otherSideRadioBtn.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent itemEvent) {
                if (itemEvent.getStateChange() == ItemEvent.SELECTED) {
                    side = "O";
                }
            }
        });
        meSideRadioBtn.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent itemEvent) {
                if (itemEvent.getStateChange() == ItemEvent.SELECTED) {
                    side = "M";
                }
            }
        });

        okButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                confirmExit();
            }
        });

    }

    @Override
    protected void processWindowEvent(WindowEvent windowEvent) {
        System.out.println("processWindowEvent");

        if (isUpdateYes) {
            System.out.println("确认更新后退出------");
            parentWindow.setEnabled(true);
            if (null != onEditGimmickUIClosedListener) {
                onEditGimmickUIClosedListener.onEditGimmickUIClosed(gimmickBean);
            }
            return;
        }

        if (windowEvent.getID() == WindowEvent.WINDOW_CLOSING) {
            int result = JOptionPane.showConfirmDialog(EditGimmickUI.this,
                                                       "直接退出将舍弃更新",
                                                       "确认操作",
                                                       JOptionPane.YES_OPTION,
                                                       JOptionPane.QUESTION_MESSAGE);
            if (JOptionPane.YES_OPTION == result) {
                System.out.println("点击关闭按钮退出");
                parentWindow.setEnabled(true);
                if (null != onEditGimmickUIClosedListener) {
                    onEditGimmickUIClosedListener.onEditGimmickUIClosed(null);
                }
                dispose();
            }
            return;
        }

        super.processWindowEvent(windowEvent);
    }

    private void initView() {
        int indexPanelWidth = frameWidth;
        int indexPanelHeight = 30;
        int separatorHeight = 5;
        int btmPanelWidth = frameWidth;
        int btmPanelHeight = frameHeight - indexPanelHeight - separatorHeight - 30;

        Box verticalBox = Box.createVerticalBox();
        verticalBox.add(getIndexPanel(indexPanelWidth, indexPanelHeight));
        verticalBox.add(getSeparator(frameWidth, separatorHeight, Color.LIGHT_GRAY));
        verticalBox.add(getBtmPanel(btmPanelWidth, btmPanelHeight));

        setContentPane(verticalBox);
    }

    private Component getIndexPanel(int panelWidth, int panelHeight) {
        JLabel indexLabel = new JLabel("索引：" + gimmickBean.getIndex());
        indexLabel.setPreferredSize(new Dimension(panelWidth, panelHeight));

        JPanel indexPanel = new JPanel();
        indexPanel.setPreferredSize(new Dimension(panelWidth, panelHeight));
        indexPanel.add(indexLabel);

        return indexPanel;
    }

    private Component getBtmPanel(int panelWidth, int panelHeight) {

        int gimmickPanelWidth = panelWidth / 3 * 2;
        int gimmickPanelHeight = panelHeight;
        int lastAndNextIndexsPanelWidth = panelWidth - gimmickPanelWidth;
        int lastAndNextIndexsPanelHeight = panelHeight;

        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        panel.setPreferredSize(new Dimension(panelWidth, panelHeight));
        panel.add(BorderLayout.WEST, getGimmickPanel(gimmickPanelWidth, gimmickPanelHeight));
        panel.add(BorderLayout.EAST, getLastAndNextIndexsPanel(lastAndNextIndexsPanelWidth, lastAndNextIndexsPanelHeight));

        return panel;
    }

    private Component getGimmickPanel(int panelWidth, int panelHeight) {
        gimmickTextArea = new JTextArea();
        gimmickTextArea.setText(gimmickBean.getGimmick());

        JScrollPane gimmickPanel = new JScrollPane(gimmickTextArea);
        gimmickPanel.setPreferredSize(new Dimension(panelWidth, panelHeight));

        return gimmickPanel;
    }

    private Component getLastAndNextIndexsPanel(int panelWidth, int panelHeight) {
        int okButtonPanelWidth = panelWidth;
        int okButtonPanelHeight = 40;
        int lastIndexsLabelWidth = panelWidth;
        int lastIndexsLabelHeight = 30;
        int nextIndexsLabelWidth = panelWidth;
        int nextIndexsLabelHeight = 30;
        int lastIndexsTextArealWidth = panelWidth;
        int lastIndexsTextAreaHeight = (panelHeight - okButtonPanelHeight - lastIndexsLabelHeight - nextIndexsLabelHeight) / 2;
        int nextIndexsTextArealWidth = panelWidth;
        int nextIndexsTextAreaHeight = lastIndexsTextAreaHeight;

        JLabel lastIndexsLabel = new JLabel("上一句");
        lastIndexsLabel.setPreferredSize(new Dimension(lastIndexsLabelWidth, lastIndexsLabelHeight));
        JPanel lastIndexsLabelPanel = new JPanel();
        lastIndexsLabelPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        lastIndexsLabelPanel.setPreferredSize(new Dimension(lastIndexsLabelWidth, lastIndexsLabelHeight));
        lastIndexsLabelPanel.add(lastIndexsLabel);

        lastIndexsTextArea = new JTextArea();
        lastIndexsTextArea.setText(UtilListOrString.listToString(gimmickBean.getOthersideIndexList()));
        JScrollPane lastIndexsTextAreaPanel = new JScrollPane(lastIndexsTextArea);
        lastIndexsTextAreaPanel.setPreferredSize(new Dimension(lastIndexsTextArealWidth, lastIndexsTextAreaHeight));

        JLabel nextIndexsLabel = new JLabel("下一句");
        nextIndexsLabel.setPreferredSize(new Dimension(lastIndexsLabelWidth, lastIndexsLabelHeight));
        JPanel nextIndexsLabelPanel = new JPanel();
        nextIndexsLabelPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        nextIndexsLabelPanel.setPreferredSize(new Dimension(nextIndexsLabelWidth, nextIndexsLabelHeight));
        nextIndexsLabelPanel.add(nextIndexsLabel);

        nextIndexsTextArea = new JTextArea();
        nextIndexsTextArea.setText(UtilListOrString.listToString(gimmickBean.getNextIndexList()));
        JScrollPane nextIndexsTextAreaPanel = new JScrollPane(nextIndexsTextArea);
        nextIndexsTextAreaPanel.setPreferredSize(new Dimension(nextIndexsTextArealWidth, nextIndexsTextAreaHeight));

        otherSideRadioBtn = new JRadioButton("对方语句 O");
        meSideRadioBtn = new JRadioButton("己方语句 M");
        if ("M".equals(gimmickBean.getSide())) {
            meSideRadioBtn.setSelected(true);
            side = "M";
        } else {
            otherSideRadioBtn.setSelected(true);
            side = "O";
        }
        ButtonGroup sideGroup = new ButtonGroup();
        sideGroup.add(otherSideRadioBtn);
        sideGroup.add(meSideRadioBtn);

        int okButtonWidth = 60;
        int okButtonHeight = okButtonPanelHeight;
        int sidePanelWidth = okButtonPanelWidth - okButtonWidth;
        int sidePanelHeight = okButtonPanelHeight;

        JPanel sidePanel = new JPanel();
        sidePanel.setPreferredSize(new Dimension(sidePanelWidth, sidePanelHeight));
        sidePanel.setLayout(new BorderLayout());
        sidePanel.add(BorderLayout.NORTH, otherSideRadioBtn);
        sidePanel.add(BorderLayout.SOUTH, meSideRadioBtn);

        okButton = new JButton("更新");
        okButton.setPreferredSize(new Dimension(okButtonWidth, okButtonHeight));

        JPanel okButtonPanel = new JPanel();
        okButtonPanel.setLayout(new BoxLayout(okButtonPanel, BoxLayout.X_AXIS));
        okButtonPanel.setPreferredSize(new Dimension(okButtonPanelWidth, okButtonPanelHeight));
        okButtonPanel.add(sidePanel);
        okButtonPanel.add(okButton);

        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        panel.setPreferredSize(new Dimension(panelWidth, panelHeight));
        panel.add(lastIndexsLabelPanel);
        panel.add(lastIndexsTextAreaPanel);
        panel.add(nextIndexsLabelPanel);
        panel.add(nextIndexsTextAreaPanel);
        panel.add(okButtonPanel);

        return panel;
    }

    private Component getSeparator(int separatorWidth, int separatorHeight, Color color) {
        JSeparator separator = new JSeparator(SwingConstants.CENTER);
        separator.setPreferredSize(new Dimension(separatorWidth, separatorHeight));
        separator.setBackground(Color.WHITE);
        separator.setForeground(color);

        return separator;
    }

    private void confirmExit() {
        int result = JOptionPane.showConfirmDialog(EditGimmickUI.this,
                                                   "执行更新",
                                                   "确认操作",
                                                   JOptionPane.YES_OPTION,
                                                   JOptionPane.QUESTION_MESSAGE);
        if (JOptionPane.YES_OPTION == result) {
            isUpdateYes = true;
            System.out.println("更新按钮点击执行退出");

            String gimmick = gimmickTextArea.getText()
                                            .trim();
            String lastIndexs = lastIndexsTextArea.getText()
                                                  .trim();
            List<String> lastIndexList = UtilListOrString.stringToList(lastIndexs);
            String nextIndexs = nextIndexsTextArea.getText()
                                                  .trim();
            List<String> nextIndexList = UtilListOrString.stringToList(nextIndexs);

            gimmickBean.setGimmick(gimmick);
            gimmickBean.setNextIndexList(nextIndexList);
            gimmickBean.setOthersideIndexList(lastIndexList);
            gimmickBean.setSide(side);

            dispose();
        }
    }

    public interface OnEditGimmickUIClosedListener {
        public void onEditGimmickUIClosed(GimmickBean gimmickBean);
    }
}
